package com.openarena.statistics;

import com.openarena.logic.utils.FacesUtil;
import com.openarena.model.Game;
import com.openarena.model.GameComment;
import com.openarena.model.GamePoint;
import com.openarena.model.Player;
import com.openarena.model.dao.GameCommentDAO;
import com.openarena.model.dao.GameDAO;
import com.openarena.model.dao.PlayerDAO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

@ManagedBean
@RequestScoped
public class GameStatistics {

    private static final Logger logger = Logger.getLogger(GameStatistics.class.getCanonicalName());
    protected final Game game;
    protected final CartesianChartModel chartPointsDuringGame;
    private String commentText;

    public GameStatistics() {
        String gameId = FacesUtil.getRequestParameter("game");
        if (gameId == null) {
            game = new GameDAO().getLastGame();
        } else {
            game = new GameDAO().getGameById(Integer.valueOf(gameId));
        }

        chartPointsDuringGame = new CartesianChartModel();
        fillPointsDuringGame();
        commentText = "";

        logger.log(Level.INFO, "Started GameStatistics Bean for game {0}", game.getId());
    }

    public Game getGame() {
        return game;
    }

    public CartesianChartModel getChartPointsDuringGame() {
        return chartPointsDuringGame;
    }

    private void fillPointsDuringGame() {
        Map<Integer, ChartSeries> chartPointsDuringGameMap = new HashMap<Integer, ChartSeries>();

        List<GamePoint> gamePoints = game.getGamePoints();
        for (GamePoint gamePoint : gamePoints) {
            Integer key = gamePoint.getPlayerId();
            ChartSeries series;
            if (chartPointsDuringGameMap.containsKey(key)) {
                series = chartPointsDuringGameMap.get(key);
            } else {
                series = new ChartSeries();
                series.set(0, 0);
                chartPointsDuringGameMap.put(key, series);
            }

            series.set(gamePoint.getGameTime(), gamePoint.getPoints());
        }

        PlayerDAO playerDAO = new PlayerDAO();
        for (Map.Entry<Integer, ChartSeries> entry : chartPointsDuringGameMap.entrySet()) {
            ChartSeries chartSeries = entry.getValue();
            chartSeries.setLabel(playerDAO.getPlayerById(entry.getKey()).getName());
            chartPointsDuringGame.addSeries(chartSeries);
        }
    }

    public String getCommentText() {
        return commentText;
    }

    public void setCommentText(String commentText) {
        this.commentText = commentText;
    }

    public void addComment() {
        if (commentText.trim().equals("")) {
            FacesUtil.addErrorMessage("Escriba su pijeo.", "");
            return;
        }

        GameComment comment = new GameComment();
        comment.setText(commentText);
        comment.setGameId(game.getId());
        comment.setIpAddress(FacesUtil.getRemoteAddress());

        try {
            new GameCommentDAO().saveComment(comment);
            commentText = "";
            FacesUtil.addSuccessMessage("Pijeo salvado.", "");
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
            FacesUtil.addErrorMessage("Error al salvar su pijeo.", "");
        }
    }

    public List<Player> getPlayerToPlayerKillsColums() {
        List<Player> players = new ArrayList<Player>();
        players.add(null);
        players.addAll(game.getRedTeam());
        return players;
    }
}
